package com.health.mapp.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.health.db.model.HospitalSpecimen;
import com.health.db.model.HospitalSpecimenDetailExample;
import com.health.db.model.HospitalSpecimenExample;
import com.health.db.service.msg.IHospitalSpecimenService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.QueryHospitalSpecimenReq;
import com.health.mapp.bean.QueryHospitalSpecimenResp;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * @author HiRoad
 *
 */
public class QueryHospitalSpecimenImpl implements ActionInterface {
	
	//private IGoodsDataService goodsDataService ;
	
	private IHospitalSpecimenService hospitalSpecimenService  ;
	
	private IUserLISTService userLISTService;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		QueryHospitalSpecimenReq req=(QueryHospitalSpecimenReq) reqBean;
		QueryHospitalSpecimenResp _resp= new QueryHospitalSpecimenResp();
		
		List<HospitalSpecimen> hospitalSpecimenList;
		
		String medicalCardNo = req.getMedicalCardNo();
		
		String specimenId = req.getSpecimenId();
		 HospitalSpecimenExample hospitalSpecimenExample = new HospitalSpecimenExample();
		 //医生编号 用户编号 订单号 取一
		 if(StringUtils.isNotBlank(medicalCardNo)){
			 if (medicalCardNo.contains(",")) {
					
				 List<String> medicalCardNos = new ArrayList<String>();
				 String[] orders = medicalCardNo.split(",");
				 for (int i = 0; i < orders.length; i++) {
					 medicalCardNos.add(orders[i]);
				}
				 hospitalSpecimenExample.setMedicalCardNos(medicalCardNos);
			}else{
				hospitalSpecimenExample.setMedicalCardNo(medicalCardNo);
			}
			 
			 _resp.setHospitalSpecimenList(hospitalSpecimenService.selectByExample(hospitalSpecimenExample));
			 
		}else if(StringUtils.isNotBlank(specimenId)){
			
			hospitalSpecimenExample.setSpecimenId(specimenId);
			hospitalSpecimenList = hospitalSpecimenService.selectByExample(hospitalSpecimenExample);
			
			if (hospitalSpecimenList.size()==1) {
				
				HospitalSpecimen hospitalSpecimenRep = hospitalSpecimenList.get(0);
				
				HospitalSpecimenDetailExample exampleDetail = new HospitalSpecimenDetailExample();
				exampleDetail.setSpecimenId(specimenId);
				
				hospitalSpecimenRep.setDetailList(hospitalSpecimenService.selectByExample(exampleDetail));
				_resp.setHospitalSpecimen(hospitalSpecimenRep);
			}
			
		}else {
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}
		_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
 
		return _resp;
	}

	public IHospitalSpecimenService getHospitalSpecimenService() {
		return hospitalSpecimenService;
	}

	public void setHospitalSpecimenService(
			IHospitalSpecimenService hospitalSpecimenService) {
		this.hospitalSpecimenService = hospitalSpecimenService;
	}

	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}

	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}

}
